package com.cskt.petpms.controller;

import com.cskt.common.condition.SearchProductCondition;
import com.cskt.common.entity.pojo.Sku;
import com.cskt.common.entity.vo.*;
import com.cskt.petpms.service.ProductService;
import jakarta.annotation.Resource;
import org.springframework.http.MediaType;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

/**
 * 商品控制器
 *
 * @author admin
 **/
@RestController
@RequestMapping(value = "/product")
public class ProductController {

    @Resource
    private ProductService productService;

    /**
     * 分页查询商品信息
     *
     * @param condition 查询条件
     * @return 响应体
     */
    @PostMapping(value = "/search", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultVO<PageVO<ProductVO>> searchProductByCondition(@RequestBody SearchProductCondition condition) {
        PageVO<ProductVO> productPage = productService.getProductVOPage(condition);
        return ResultVO.ok(productPage);
    }

    /**
     * 查询商品详情
     *
     * @param spuId 商品id
     * @return 响应体
     */
    @GetMapping(value = "/detail")
    public ResultVO<ProductDetailVO> getProductDetailById(Long spuId) {
        ProductDetailVO productDetailVO = productService.getProductDetailById(spuId);
        if (ObjectUtils.isEmpty(productDetailVO)) {
            return ResultVO.error();
        } else {
            return ResultVO.ok(productDetailVO);
        }
    }

    /**
     * 提供微服务，被pet-oms模块添加购物车接口调用
     * @param skuId 商品规格id
     * @return 商品规格
     */
    @GetMapping(value = "/sku/{skuId}")
    public ResultVO<Sku> getSkuById(@PathVariable Long skuId) {
        Sku sku = productService.getSkuBySkuId(skuId);
        return ObjectUtils.isEmpty(sku) ? ResultVO.error(StatusEnum.PRODUCT_SPU_EMPTY) :
                ResultVO.ok(sku);
    }
    
}
